package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.List;

//差旅报销单保存插件；
//说明：是否超标和报销标准赋值逻辑
public class TripReimburseSaveOp extends AbstractOperationServicePlugIn {

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);

        OperationResult operationResult = getOperationResult();
        if (!operationResult.isSuccess()) {
            return;
        }

        DynamicObject[] dataEntities = e.getDataEntities();
        List<DynamicObject> modifiedEntities = new ArrayList<>();

        for (DynamicObject model : dataEntities) {
            long id = model.getLong("id");
            QFilter f1 = new QFilter("id", "=", id);
            DynamicObject er_tripreimbursebill = BusinessDataServiceHelper.loadSingle("er_tripreimbursebill", new QFilter[]{f1});
            DynamicObjectCollection tripEntryCollection = er_tripreimbursebill.getDynamicObjectCollection("tripentry");//行程信息
            for (int i = 0; i < tripEntryCollection.size(); i++) {
                DynamicObject tripEntry = tripEntryCollection.get(i);
                DynamicObjectCollection entryEntityCollection = tripEntry.getDynamicObjectCollection("entryentity");//差旅明细
                for (int i1 = 0; i1 < entryEntityCollection.size(); i1++) {
                    DynamicObject entryEntity = entryEntityCollection.get(i);
                    String overdesc = entryEntity.getString("overdesc");//超标说明
                    DynamicObject expenseItem = entryEntity.getDynamicObject("expenseitem");//差旅项目
                    if (expenseItem != null) {
                        String expenseItemNumber = expenseItem.getString("number");
                        if ("010".equals(expenseItemNumber) || "011".equals(expenseItemNumber) || "012".equals(expenseItemNumber)) {
                            //误餐补贴分录
                            er_tripreimbursebill.set("qeug_stdover", false);//是否超标/否
                            er_tripreimbursebill.set("qeug_combofield1", "1");//报销标准（标准内）
                        } else {
                            if (overdesc == null || "".equals(overdesc)) {
                                er_tripreimbursebill.set("qeug_stdover", false);//是否超标/否
                                er_tripreimbursebill.set("qeug_combofield1", "1");//报销标准（标准内）
                            }else{
                                //存在超标说明时是否超标赋值为是
                                er_tripreimbursebill.set("qeug_stdover", true);//是否超标/是
                                er_tripreimbursebill.set("qeug_combofield1", "0");//报销标准（标准外）
                            }
                        }
                    }
                }
            }
            modifiedEntities.add(er_tripreimbursebill);
        }
        if (!modifiedEntities.isEmpty()) {
            try {
                SaveServiceHelper.save(modifiedEntities.toArray(new DynamicObject[0]));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
